#encoding=utf8
'''
Created on 2012-10-17

@author: Administrator
'''
from dispatch.Processor import Processor;
from pub import Func, UserDict, RoomDict;
from pub.Config import Config;

class RISGClass(Processor):
    '''
    指令：RISG(room idiom start game) 
    备注：开始游戏
    格式：c-s:cmd#cmdID#roomID#userID
     s-c:RISG #cmdID# result #roomId#userID# userNickName# msg
    '''
    def __init__(self,):
        Processor.__init__(self);
        self.__roomId = 0;
        self.__userId = 0;
        
    def dataValidtor(self, cmdContent):
        if not cmdContent:
            return False;
        paraLen = len(cmdContent);
        if paraLen == 2:
            self.__roomId = Func.strToInt(cmdContent[0], Config.I_SYS_ZERO);
            self.__userId = Func.strToInt(cmdContent[1], Config.I_SYS_ZERO);
            return True;        
        return False;
    def outData(self,):
        room = RoomDict.getRoomFromDictById(self.__roomId);
        if not room:
            Func.sendError(self._sockObj, self._cmdId, Config.ERROR_MESSAGE_ROOM_DETAIL_NULL + ",roomId:" + Func.toStr(self.__roomId, 0));
            return False;
        user = UserDict.getUserFromUserDictById(self.__userId);
        if not user:
            Func.sendError(self._sockObj, self._cmdId, Config.ERROR_MESSAGE_USER_DETAIL_NULL + ",userId:" + Func.toStr(self.__userId, 0));
            return False;    
        if room.get_room_owner() != user:
            Func.sendError(self._sockObj, self._cmdId, Config.ERROR_MESSAGE_ROOM_OWER_NULL + ",userId:" + Func.toStr(self.__userId, 0));
            return False;    
                
        userList = room.get_user_list();
        userNum = len(userList);
        sendStr = "";
        result = Config.I_SYS_ONE;
        msg = Config.ROOM_MESSAGE_CODE_START_SUSS;
        if userNum < 2:
            result = Config.I_SYS_ZERO;
            msg = Config.ROOM_MESSAGE_CODE_START_LOWER_LIMIT;            
            
        sendStr = self.setPackage([result, self.__roomId, self.__userId, user.get_user_nick_name(), msg]);
        room.set_state(Config.I_ROOM_STATE_START);
        room.set_used_idiom({});
        room.set_current_word(room.get_first_name());
        
        for userTmp in userList:
            userTmp.set_user_game_state(Config.I_USER_GAME_STATE_OK);
        RoomDict.sendInfoToRoomAllUser(roomObj=room, cmd="", msg=sendStr, isPackage=False);
        return True;   
    
